﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using T.ApiResults;
using T.Materials.Domain.Dto;
using T.Materials.Domain.Model;
using T.Materials.Read.API.ApplicationRead.Command.CountryCommand;
using T.Materials.Read.API.ApplicationRead.Command.EconomizeCommand;
using T.Materials.Read.API.ApplicationRead.Command.MaterialStoreCommand;
using static T.Materials.Domain.Enum.Enums;

namespace T.Materials.Read.API.Controllers
{
    /// <summary>
    /// API控制器--显示物资与入库
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class ShowMaterialStoreController : ControllerBase
    {
        /// <summary>
        /// 命令总线
        /// </summary>
        protected readonly IMediator mediator;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mediator">命令总线</param>
        public ShowMaterialStoreController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        /// <summary>
        /// 获取审批状态枚举
        /// </summary>
        /// <returns>返回审批状态枚举</returns>
        [HttpGet]
        public IActionResult GetApprovalStatusEnum()
        {
            try
            {
                List<object> list = new List<object>();
                foreach (var item in Enum.GetValues<ApprovalStatus>())
                {
                    list.Add(new
                    {
                        enumId = item,
                        enumName = item.ToString()
                    });
                }
                return Ok(list);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 查询物资列表
        /// </summary>
        /// <param name="command">查询物资入库命令</param>
        /// <returns>返回物资列表</returns>
        [HttpGet]
        public async Task<ApiResult<ListMaterialStoreDto<MaterialStoreDto>>> GetMaterialStoreList([FromQuery] ShowMaterialStoreCommand command)
        {
            ApiResult<ListMaterialStoreDto<MaterialStoreDto>> result = new ApiResult<ListMaterialStoreDto<MaterialStoreDto>>();
            try
            {
                result.Data = await mediator.Send(command);
            }
            catch (Exception)
            {
                result.Code = -1;
                result.Message = "查询物资列表失败";
                throw;
            }
            return result;
        }

        /// <summary>
        /// 查询入库列表
        /// </summary>
        /// <param name="command">查询物资入库命令</param>
        /// <returns>返回物资入库列表</returns>
        [HttpGet]
        public async Task<ApiResult<ListMaterialStoreDto<MaterialStoreDto>>> GetStoreList([FromQuery] ShowStoreCommand command)
        {
            ApiResult<ListMaterialStoreDto<MaterialStoreDto>> result = new ApiResult<ListMaterialStoreDto<MaterialStoreDto>>();
            try
            {
                result.Data = await mediator.Send(command);
            }
            catch (Exception)
            {
                result.Code = -1;
                result.Message = "查询入库列表失败";
                throw;
            }
            return result;
        }

        /// <summary>
        /// 反填物资入库
        /// </summary>
        /// <param name="command">反填物资入库命令</param>
        /// <returns>返回物资入库</returns>
        [HttpGet]
        public async Task<ApiResult<MaterialStore>> ReverseMaterialStore([FromQuery] ReverseMaterialStoreCommand command)
        {
            ApiResult<MaterialStore> result = new ApiResult<MaterialStore>();
            try
            {
                result.Data = await mediator.Send(command);
            }
            catch (Exception)
            {
                result.Code = -1;
                result.Message = "反填物资入库失败";
                throw;
            }
            return result;
        }

        /// <summary>
        /// 查询指定Id物资入库详情
        /// </summary>
        /// <param name="command">查询指定Id物资入库详情命令</param>
        /// <returns>返回物资入库详情</returns>
        [HttpGet]
        public async Task<ApiResult<List<MaterialStoreDto>>> GetMaterialStoreByIdList([FromQuery] ShowStoreByIdCommand command)
        {
            ApiResult<List<MaterialStoreDto>> result = new ApiResult<List<MaterialStoreDto>>();
            try
            {
                result.Data = await mediator.Send(command);
            }
            catch (Exception)
            {
                result.Code = -1;
                result.Message = "查询指定Id物资入库详情失败";
                throw;
            }
            return result;
        }

        /// <summary>
        /// 查询国家列表
        /// </summary>
        /// <param name="command">查询国家列表命令</param>
        /// <returns>返回国家列表</returns>
        [HttpGet]
        public async Task<ApiResult<List<Country>>> GetCountryList([FromQuery] ShowCountryCommand command)
        {
            ApiResult<List<Country>> result = new ApiResult<List<Country>>();
            try
            {
                result.Data = await mediator.Send(command);
            }
            catch (Exception)
            {
                result.Code = -1;
                result.Message = "查询国家列表失败";
                throw;
            }
            return result;
        }

        /// <summary>
        /// 查询省/州列表
        /// </summary>
        /// <param name="command">查询省/州列表命令</param>
        /// <returns>返回省/州列表</returns>
        [HttpGet]
        public async Task<ApiResult<List<Economize>>> GetEconomizeList([FromQuery] ShowEconomizeCommand command)
        {
            ApiResult<List<Economize>> result = new ApiResult<List<Economize>>();
            try
            {
                result.Data = await mediator.Send(command);
            }
            catch (Exception)
            {
                result.Code = -1;
                result.Message = "查询省/州列表失败";
                throw;
            }
            return result;
        }

        /// <summary>
        /// 查询物资名称列表(已通过)
        /// </summary>
        /// <param name="command">查询物资名称列表命令</param>
        /// <returns>返回物资名称列表</returns>
        [HttpGet]
        public async Task<ApiResult<List<MaterialStore>>> GetMaterialStoreName([FromQuery] MaterialStoreNameCommand command)
        {
            ApiResult<List<MaterialStore>> result = new ApiResult<List<MaterialStore>>();
            try
            {
                result.Data = await mediator.Send(command);
            }
            catch (Exception)
            {
                result.Code = -1;
                result.Message = "查询物资名称列表失败";
                throw;
            }
            return result;
        }
    }
}
